package cn.edu.ecnu.gis.client.view;

import cn.edu.ecnu.gis.client.model.GISEnable;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.SimplePanel;
import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.UserPref;
import com.google.gwt.user.client.ui.Image;

public class GadgetContainerView extends SimplePanel implements SourcesMouseEvents {

    public static interface TitleBarImageBundle extends ImageBundle {

        /**
         * @gwt.resource close.gif
         */
        public AbstractImagePrototype closeIcon();

        /**
         * 
         * @return
         */
        public AbstractImagePrototype gisEnabledIcon();

        /**
         * 
         * @return
         */
        public AbstractImagePrototype gisDisEnabledIcon();

        /**
         * @gwt.resource minimize1.gif
         */
        public AbstractImagePrototype minimize1Icon();

        /**
         * @gwt.resource minimize2.gif
         */
        public AbstractImagePrototype minimize2Icon();

        /**
         * @gwt.resource refresh.gif
         */
        public AbstractImagePrototype refreshIcon();
    }
    public static TitleBarImageBundle titleBarImages = (TitleBarImageBundle) GWT.create(TitleBarImageBundle.class);
    private Gadget child;
    private HorizontalPanel titleBar = new HorizontalPanel();
    private VerticalPanel mainLayout = new VerticalPanel();
    private Label title = new Label();
    private Hyperlink edit = new Hyperlink("edit", "");
    private Image gisEnabledIcon = titleBarImages.gisEnabledIcon().createImage();
    private Image gisDisEnabledIcon = titleBarImages.gisDisEnabledIcon().createImage();
    private ToggleButton minimizeButton = new ToggleButton(titleBarImages.minimize1Icon().createImage(), titleBarImages.minimize2Icon().createImage());
    private PushButton refreshButton = new PushButton(titleBarImages.refreshIcon().createImage());
    private PushButton closeButton = new PushButton(titleBarImages.closeIcon().createImage());
    private FlexTable editPanel = new FlexTable();
    private SimplePanel childContainer = new SimplePanel();
    private boolean open = true;
    private boolean editopen = false;

    public GadgetContainerView(Gadget c) {
        child = c;
        buildTitleBar();
        buildMainLayout();
        mainLayout.setWidth("100%");
        RoundedPanel round = new RoundedPanel("#eaecec", mainLayout, RoundedPanel.ROUND_TOP);
        round.setWidth("100%");
        setWidget(round);
        child.refresh();

        //set style names
        setStyleName("gwtapps-GadgetContainer");
        mainLayout.setStyleName("gwtapps-GadgetContainerPanel");
        titleBar.setStyleName("gwtapps-GadgetContainerTitleBar");
        editPanel.setStyleName("gwtapps-GadgetContainerEditPanel");
        title.setStyleName("gwtapps-GadgetContainerTitle");
        childContainer.setStyleName("gwtapps-GadgetContainerBody");
    }

    protected void buildMainLayout() {
        RoundedPanel roundTitle = new RoundedPanel("#E6F1D2", titleBar, RoundedPanel.ROUND_TOP);

        roundTitle.setWidth("100%");
        mainLayout.add(roundTitle);
        if (child.getGadgetClass().getUserPrefsCount() > 0) {
            mainLayout.add(editPanel);
            editPanel.setWidth("100%");
            buildEditPanel();
        }
        childContainer.setWidget(child);
        mainLayout.add(childContainer);
    }

    protected void buildTitleBar() {
        int column = 0;
        
        if (child.isGisEnabled())  {
            titleBar.add(gisEnabledIcon);
        } else {
            titleBar.add(gisDisEnabledIcon);
        }

        titleBar.add(minimizeButton);
        titleBar.add(title);
        titleBar.add(refreshButton);
        if (child.getGadgetClass().getUserPrefsCount() > 0) {
            titleBar.add(edit);
        }
        titleBar.add(closeButton);
        titleBar.setCellWidth(title, "100%");
        titleBar.setWidth("100%");
        title.setWidth("100%");
        title.setText(child.getTitle());
        buildClickListeners();
    }

    protected void buildClickListeners() {
        edit.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                toggleEdit();
            }
        });
        refreshButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                child.refresh();
            }
        });
        closeButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                if (Window.confirm("Remove " + child.getGadgetClass().getName() + " from your page?")) {
                    getParent().removeFromParent();
                    child.getGadgetListener().onInterfaceChange();
                }
            }
        });
        minimizeButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                if (minimizeButton.isDown()) {
                    child.setVisible(false);
                } else {
                    child.setVisible(true);
                }
                open = !open;
            }
        });
    }

    protected void buildEditPanel() {
        editPanel.setVisible(editopen);
        editPanel.setStyleName("gwtapps-GadgetContainerEditPanel");
        for (int i = 0; i < child.getGadgetClass().getUserPrefsCount(); ++i) {
            final UserPref up = child.getGadgetClass().getUserPref(i);
            editPanel.setText(i, 0, up.getName() + ":");
            Object value = child.getUserPrefValue(up);
            if (value instanceof String) {
                final TextBox editbox = new TextBox();
                editbox.setText((String) value);
                editbox.addChangeListener(new ChangeListener() {

                    public void onChange(Widget sender) {
                        child.setUserPrefValue(up, editbox.getText());
                    }
                });
                editPanel.setWidget(i, 1, editbox);
                editPanel.getCellFormatter().setWidth(i, 1, "100%");
                editbox.setWidth("100%");
            }
        }
    }

    public void toggleEdit() {
        editopen = !editopen;
        editPanel.setVisible(editopen);
        if (!editopen) {
            edit.setText("edit");
            child.refresh();
            child.getGadgetListener().onInterfaceChange();
        } else {
            edit.setText("done");
        }
    }

    public void addMouseListener(MouseListener listener) {
        title.addMouseListener(listener);
    }

    public void removeMouseListener(MouseListener listener) {
        title.removeMouseListener(listener);
    }

    public Gadget getGadget() {
        return child;
    }
}
